package com.yswy.app.controller;

import com.yswy.app.dto.LoginUserDto;
import com.yswy.app.service.*;
import com.yswy.common.model.ResultModel;
import com.yswy.common.utils.UserLockUtils;
import com.yswy.domain.entity.UserJlAdLog;
import com.yswy.domain.vo.dto.TgDeviceKs;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;
import java.util.Map;
import java.util.zip.CRC32;

import static com.yswy.domain.common.util.TokenUtils.*;

@RestController
@RequestMapping("/tg")
@Api(description = "推广相关")
@Slf4j
public class TgController {

    @Autowired
    private TgCallBackIdService tgCallBackIdService ;
    @Autowired
    private TgCallBackKsIdService tgCallBackKsIdService ;
    @Autowired
    private TgChannleService tgChannleService ;
    @Autowired
    private UserJlAdLogService userJlAdLogService ;


    /** 推广相关 **/
    @PostMapping("/handleCallback")
    @ApiOperation(value = "推广相关")
    public ResultModel handleCallback(@RequestBody Map<String , String> map ,HttpServletRequest request ) {
        tgCallBackIdService.addTgCallBackId(map ,getIP(request) );
        return ResultModel.ok() ;
    }


//
//    /** 推广相关 **/
//    @GetMapping("/handleCallback")
//    @ApiOperation(value = "推广相关")
//    public void handleCallback(
//            @RequestParam(value = "type" ,required = false ,defaultValue = "2") Integer type ,
//            @RequestParam(value = "channle" ,required = false ,defaultValue = "defChannle") String channle ,
//            HttpServletRequest request, HttpServletResponse response
//    ) throws IOException {
//
//        log.info("推广相关channle = "+channle+" type = "+type);
//        tgCallBackIdService.addTgCallBackId(channle ,request  ,type );
//        response.setContentType("text/html;charset=utf-8");
//        response.getWriter().write("success");
//        response.getWriter().flush();
//        response.getWriter().close();
//    }


    /** 推广相关 **/
    @GetMapping("/ksCallback")
    @ApiOperation(value = "推广相关(快手)")
    public void ksCallback(
            @RequestParam(value = "channle" ,required = false ,defaultValue = "defChannle") String channle ,
            @RequestParam(value = "DID" ,required = false) String DID,
            @RequestParam(value = "AID" ,required = false) String AID,
            @RequestParam(value = "CID" ,required = false) String CID,
            @RequestParam(value = "DNAME" ,required = false) String DNAME,
            @RequestParam(value = "UNITNAME" ,required = false) String UNITNAME,
            @RequestParam(value = "OAID2" ,required = false) String OAID2,
            @RequestParam(value = "IMEI2" ,required = false) String IMEI2,
            @RequestParam(value = "IDFA2" ,required = false) String IDFA2,
            @RequestParam(value = "MAC2" ,required = false) String MAC2,
            @RequestParam(value = "ANDROIDID2" ,required = false) String ANDROIDID2,
            @RequestParam(value = "UA" ,required = false) String UA,
            @RequestParam(value = "OS" ,required = false) String OS,
            @RequestParam(value = "MODEL" ,required = false) String MODEL,
            @RequestParam(value = "CALLBACK" ,required = false) String CALLBACK,
            @RequestParam(value = "IP" ,required = false)  String IP,
            HttpServletRequest request, HttpServletResponse response
    ) throws IOException {
        log.info("推广相关channle = "+channle);


//        https://bkservice.dyh2024.top/tg/ksCallback?channle=ksaddiction-001&type=2&AID=__AID__&CID=__CID__&DNAME=__DNAME__&UNITNAME=__UNITNAME__&OAID2=__OAID2__&IMEI2=__IMEI2__&IDFA2=__IDFA2__&MAC2=__MAC2__&ANDROIDID2=__ANDROIDID2__&UA=__UA__&OS=__OS__&MODEL=__MODEL__&CALLBACK=__CALLBACK__&IP=__IP__


        TgDeviceKs tgDeviceKs = new TgDeviceKs( DID, AID, CID
                , DNAME, UNITNAME, OAID2, IMEI2, IDFA2
                , MAC2, ANDROIDID2, UA
                , OS, MODEL, CALLBACK, IP);

        tgCallBackKsIdService.addTgCallBackKsId(channle ,tgDeviceKs );
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("success");
        response.getWriter().flush();
        response.getWriter().close();
    }

    @GetMapping("/saveUserJlAdLog")
    @ApiOperation(value = "推广相关")
    public void saveUserJlAdLog(
            @RequestParam(value = "user_id" ) Long user_id,
            @RequestParam(value = "trans_id" ) String trans_id,
            @RequestParam(value = "reward_amount" ,required = false) String reward_amount,
            @RequestParam(value = "reward_name" ) String reward_name,
            @RequestParam(value = "prime_rit" ) String prime_rit,
            @RequestParam(value = "mediation_rit" ) String mediation_rit,
            @RequestParam(value = "sign" ) String sign,
            @RequestParam(value = "extra",required = false ) String extra
            , HttpServletResponse response) {
        UserJlAdLog userJlAdLog = UserJlAdLog.builder()
                .userId(user_id)
                .transId(trans_id)
                .rewardName(reward_name)
                .primeRit(prime_rit)
                .mediationRit(mediation_rit)
                .sign(sign)
                .extra(extra)
                .isReceive(false)
                .createTime(new Date())
                .build();
        String s = userJlAdLogService.saveUserJlAdLog(userJlAdLog);
        try {
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write(s);
            response.getWriter().flush();
            response.getWriter().close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    @GetMapping("/delDeduction")
    @ApiOperation(value = "推广相关")
    public void delDeduction(
            @RequestParam(value = "channle" ) String channle,
            @RequestParam(value = "star" ,required = false) String star,
            @RequestParam(value = "stop" ,required = false) String stop) {
        tgChannleService.delDeduction(channle,star ,stop);
    }

}
